// pages/personalCenter/checkIn/newCheckIn/newCheckIn.js
var transDate = require('../../../../module/transDate');
var myLongitude = 0;
var myLatitude = 0;
var app = getApp();
//存放所有用户的经纬度信息
var checkForm = [];
//循环刷新停止码
var stopInterval = '';

Page({
    /**
     * 自定义函数
     */

    //获取自己的位置
    locationInit: function() {
        var that = this;
        wx.getLocation({
            success: function(res) {
                myLongitude = res.longitude;
                myLatitude = res.latitude;
                that.setData({
                    myLongitude: myLongitude
                });
                that.setData({
                    myLatitude: myLatitude
                });
            },
        });
    },

    //发起签到并收回签到时间选择菜单
    newCheck: function(delayTime) {
        var that = this;
        //收回签到时间选择菜单
        this.setData({
            show_check_time: !this.data.show_check_time
        });
        //创建签到
        checkForm[0] = {};
        checkForm[0].item = 'header';
        checkForm[0].longitude = myLongitude;
        checkForm[0].latitude = myLatitude;
        checkForm[0].user_id = app.globalData.user_info.user_id;
        var time = Date.now().toString();
        checkForm[0].check_num = time.slice(9);
        checkForm[0].check_id = 'c' + time.slice(9);
        checkForm[0].time = transDate(time);
        checkForm[0].stats = 1;
        checkForm[0].user_name = app.globalData.user_info.user_name;
        checkForm[0].start_time = checkForm[0].time.time;
        console.log(checkForm);
        wx.request({
            url: `https://wx.jingqi.ink/wxapp/checkin`,
            data: checkForm,
            method: 'POST',
            success(res) {
                //提示创建签到表成功
                wx.showModal({
                    title: '提示',
                    content: `已经创建新的签到，签到四位数暗号是【${checkForm[0].check_num}】，签到者将输入同样的四位数暗号进行签到`,
                    showCancel: false,
                    confirmText: '我知道了',
                    success(res) {
                        if (res.confirm == true) {
                            //生成签到记录
                            //给页面变量赋值
                            checkForm[1] = {};
                            checkForm[1].item = 'content';
                            that.setData({
                                form: checkForm
                            })
                            //点击确定后再给计时器赋值
                            that.setData({
                                check_time_target: Date.now() + delayTime
                            });
                            //开启定时器
                            that.setData({
                                clearTimer: false
                            });
                            //循环获取最新数据
                            stopInterval = setInterval(function() {
                                wx.request({
                                    url: `https://wx.jingqi.ink/wxapp/checkin/999/${checkForm[0].check_num}`,
                                    method: 'GET',
                                    success(res) {
                                        //给页面变量赋值
                                        checkForm[1].content = JSON.parse(res.data[0].in_user_id);
                                        that.setData({
                                            form: checkForm
                                        })
                                    }
                                })
                            }, 1000);
                        }
                    }
                })
            }
        })
    },

    /**
     * 页面函数
     */

    //发起签到后动作，弹出选择签到时间提示框
    newCheckIn: function() {
        this.setData({
            show_check_time: !this.data.show_check_time
        });
    },

    //退出签到时间选择页面
    check_time_cancel: function() {
        this.setData({
            show_check_time: !this.data.show_check_time
        })
    },

    //点击签到时间选择
    check_time_click: function(e) {
        switch (e.detail.index) {
            case 0:
                wx.showModal({
                    title: '提示',
                    content: '请选择签到时长',
                    confirmText: '去选择',
                    showCancel: false,
                })
                break;
            case 1:
                this.newCheck(30000);
                break;
            case 2:
                this.newCheck(60000);
                break;
            case 3:
                this.newCheck(120000);
                break;
            case 4:
                this.newCheck(180000);
                break;
            case 5:
                this.newCheck(300000);
                break;
        }
        //取消显示新建签到按钮
        this.setData({
            show_start_check: false
        });
        //显示分享
        this.setData({
            show_share_check: true
        });
    },

    //签到时间结束
    check_time_end: function() {
        clearInterval(stopInterval);
        checkForm[0].stats = 0;
        console.log(checkForm);
        wx.request({
            url: `https://wx.jingqi.ink/wxapp/checkin`,
            data: checkForm,
            method: 'POST',
            success(res) {
                wx.showModal({
                    title: '提示',
                    content: '本次签到已经结束，您可在页面下方查看签到数据',
                    showCancel: false,
                    confirmText: '我知道了'
                })
            }
        })
    },
    /**
     * 页面的初始数据
     */
    data: {
        //签到时间提示框
        show_check_time: false,
        check_time_content: [{
                name: '请选择签到时长'
            },
            {
                name: '30秒'
            }, {
                name: '1分钟'
            }, {
                name: '2分钟'
            }, {
                name: '3分钟'
            }, {
                name: '5分钟'
            }
        ],
        //初始签到时间置零
        check_time_target: 0,
        clearTimer: true,
        //默认显示新建签到按钮
        show_start_check: true,
        //默认不显示分享按钮
        show_share_check: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        //页面显示后加载当前位置
        this.locationInit();
        wx.setNavigationBarTitle({
            title: '发起签到',
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        var num = checkForm[0].check_num;
        return {
            title: `签到暗号[${num}]`,
            path: 'pages/personalCenter/checkIn/joinCheckIn/joinCheckIn?temp=' + JSON.stringify(num),
        }
    }
})